package com.stylefeng.guns.modular.flagglory.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.common.persistence.model.Chapter;
import com.stylefeng.guns.modular.flagglory.service.IChapterService;

import javax.validation.Valid;
import java.util.List;

/**
 * 学习园地章节控制器
 *
 * @author fengshuonan
 * @Date 2018-02-05 09:28:22
 */
@Controller
@RequestMapping("/chapter")
public class ChapterController extends BaseController {

    private String PREFIX = "/flagGlory/chapter/";

    @Autowired
    private IChapterService chapterService;

    /**
     * 跳转到学习园地章节首页
     */
    @RequestMapping("index")
    public String index(Integer bookId, Model model) {
        model.addAttribute("bookId", bookId);
        return PREFIX + "chapter.html";
    }

    /**
     * 跳转到添加学习园地章节
     */
    @RequestMapping("/chapter_add")
    public String chapterAdd(Integer bookId, Model model) {
        model.addAttribute("bookId", bookId);
        return PREFIX + "chapter_add.html";
    }

    /**
     * 跳转到修改学习园地章节
     */
    @RequestMapping("/chapter_update/{chapterId}")
    public String chapterUpdate(@PathVariable Integer chapterId, Model model, Integer bookId) {
        Chapter chapter = chapterService.selectById(chapterId);
        model.addAttribute("item", chapter);
        model.addAttribute("bookId", bookId);
        LogObjectHolder.me().set(chapter);
        return PREFIX + "chapter_edit.html";
    }

    /**
     * 获取学习园地章节列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Integer bookId) {
        EntityWrapper<Chapter> entityWrapper = new EntityWrapper<>();
        entityWrapper.where("bookid=" + bookId);
        return chapterService.selectList(entityWrapper);
    }

    /**
     * 新增学习园地章节
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@Valid Chapter chapter, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new Exception(result.getFieldError().getDefaultMessage());
        }
        chapterService.insert(chapter);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除学习园地章节
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer chapterId) {
        chapterService.deleteById(chapterId);
        return SUCCESS_TIP;
    }

    /**
     * 修改学习园地章节
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@Valid Chapter chapter, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new Exception(result.getFieldError().getDefaultMessage());
        }
        chapterService.updateById(chapter);
        return super.SUCCESS_TIP;
    }

    /**
     * 学习园地章节详情
     */
    @RequestMapping(value = "/detail/{chapterId}")
    @ResponseBody
    public Object detail(@PathVariable("chapterId") Integer chapterId) {
        return chapterService.selectById(chapterId);
    }
}
